package com.guigu.gulimember.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.gulicommon.constant.enumes.res.AddResCode;
import com.guigu.gulicommon.constant.enumes.res.AuthResCode;
import com.guigu.gulicommon.constant.enumes.res.SelectResCode;
import com.guigu.gulicommon.context.UserContext;
import com.guigu.gulicommon.entity.dto.RegisterByGiteeDto;
import com.guigu.gulicommon.entity.pojo.res.Res;
import com.guigu.gulicommon.entity.vo.UserInfoVo;
import com.guigu.gulicommon.exception.BaseException;
import com.guigu.gulimember.entity.UmsMember;
import com.guigu.gulimember.mapper.UmsMemberMapper;
import com.guigu.gulimember.service.IUmsMemberService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.guigu.gulicommon.constant.enumes.EnableStatus.ENABLING;

/**
 * 会员 服务实现类
 *
 * @author 陈俊杰
 * @since 2024-08-03
 */
@Service
@Log4j2
public class UmsMemberServiceImpl extends ServiceImpl<UmsMemberMapper, UmsMember> implements IUmsMemberService {


    /**
     * 通过Gitee信息注册会员
     *
     * @param dto Gitee信息
     * @return 用户信息
     */
    @Override
    public Res<UserInfoVo> registerByGitee(RegisterByGiteeDto dto) {
        //1.根据登陆账号查询数据库是否存在该用户,如果存在则抛异常
        //1.1 拼接查询sql
        LambdaQueryWrapper<UmsMember> umsMemberLambdaQueryWrapper = new LambdaQueryWrapper<UmsMember>()
                .eq(UmsMember::getUsername, dto.getUsername());
        //1.2 执行查询
        List<UmsMember> umsMembers = this.baseMapper.selectList(umsMemberLambdaQueryWrapper);
        if (!umsMembers.isEmpty()) {

            //1.3 存在则抛异常
            UserInfoVo userInfoVo = new UserInfoVo();

            BeanUtils.copyProperties(umsMembers.get(0), userInfoVo);
            log.info("用户信息:{}", userInfoVo);
            return new Res<>(AddResCode.INSERT_MEMBER_FAIL_DATA_IS_EXIST, userInfoVo);
        }
        //2. 使用springboot自带加密方式生成加密密码
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password = encoder.encode("123456");
        //3. 封装用户信息
        UmsMember umsMember = new UmsMember()
                .setNickname(dto.getNikeName())
                .setUsername(dto.getUsername())
                .setId(dto.getId())
                .setPassword(password)
                .setStatus(ENABLING)
                .setHeader(dto.getAvatarUrl());
        this.baseMapper.insert(umsMember);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(umsMember, userInfoVo);
        return new Res<>(AddResCode.INSERT_MEMBER_SUCCESS, userInfoVo);
    }

    /**
     * 根据账号密码获取用户信息
     *
     * @param userName 账号
     * @param password 密码
     * @return 用户信息
     */
    @Override
    public Res<UserInfoVo> getUserByUserNameAndPassword(String userName, String password) {
        //1.根据账号查询数据库
        //1.1 拼接查询sql
        LambdaQueryWrapper<UmsMember> umsMemberLambdaQueryWrapper = new LambdaQueryWrapper<UmsMember>()
                .eq(UmsMember::getUsername, userName)
                .eq(UmsMember::getStatus, ENABLING);
        //1.2 执行查询
        List<UmsMember> umsMembers = this.baseMapper.selectList(umsMemberLambdaQueryWrapper);
        //1.3 判断是否查询到数据
        if (!umsMembers.isEmpty()) {
            if (new BCryptPasswordEncoder().matches(password, umsMembers.get(0).getPassword())) {
                //1.3.1 如果存在则判断密码是否正确
                UserInfoVo userInfoVo = new UserInfoVo();
                BeanUtils.copyProperties(umsMembers.get(0), userInfoVo);
                return new Res<>(SelectResCode.SELECT_MEMBER_INFO_SUCCESS, userInfoVo);
            } else {
                //1.3.2 如果密码错误则抛异常
                throw new BaseException(AuthResCode.MEMBER_NAME_OR_PASSWORD_ERROR);
            }
        } else {
            //1.4 如果不存在则抛异常
            throw new BaseException(SelectResCode.SELECT_MEMBER_INFO_FAIL_DATA_IS_NOT_EXIST);
        }
    }

    /**
     * 根据账号查询用户是否存在
     *
     * @param userName 账号
     * @return 会员账号存在状态
     */
    @Override
    public Res<UserInfoVo> existsByUserName(String userName) {
        //1.根据账号查询数据库
        LambdaQueryWrapper<UmsMember> lambdaQueryWrapper = new LambdaQueryWrapper<UmsMember>().eq(UmsMember::getUsername, userName)
                .eq(UmsMember::getStatus, ENABLING);
        List<UmsMember> UserInfoVoList = this.baseMapper.selectList(lambdaQueryWrapper);
        if (!UserInfoVoList.isEmpty()) {
            //2.1 如果存在则返回用户信息
            UserInfoVo userInfoVo = new UserInfoVo();
            BeanUtils.copyProperties(UserInfoVoList.get(0), userInfoVo);
            return new Res<>(SelectResCode.SELECT_MEMBER_INFO_SUCCESS, userInfoVo);
        } else {
            //2.2 如果不存在则抛异常
            throw new BaseException(SelectResCode.SELECT_MEMBER_INFO_FAIL_DATA_IS_NOT_EXIST);
        }
    }

    /**
     * 根据Id查询会员信息
     *
     * @return 会员信息
     */
    @Override
    public Res<UserInfoVo> getInfoById() {
        Long id = UserContext.getInstance().getCurrentUser().getId();
        UmsMember umsMember = this.baseMapper.selectById(id);
        if (umsMember == null) {
            throw new BaseException(SelectResCode.SELECT_MEMBER_INFO_FAIL_DATA_IS_NOT_EXIST);
        }
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(umsMember, userInfoVo);
        return new Res<>(SelectResCode.SELECT_MEMBER_INFO_SUCCESS, userInfoVo);
    }
}
